package ${table.basePackage}.entity;

<#-- 自动收集需要导入的类型并分类 -->
<#assign javaImports = []>
<#assign thirdPartyImports = []>
<#assign projectImports = []>

<#list columns as c>
  <#-- 跳过BaseEntity中已有的字段 -->
  <#assign skipField = false>
  <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version" || c.columnName?lower_case == "remark" || c.columnName?lower_case == "remarks")>
    <#assign skipField = true>
  </#if>
  
  <#if !skipField && c.javaType?? && c.javaType != "String" && c.javaType?contains(".")>
    <#if c.javaType?starts_with("java.") || c.javaType?starts_with("javax.")>
      <#assign javaImports = javaImports + [c.javaType]>
    <#elseif c.javaType?starts_with("com.kexio.")>
      <#assign projectImports = projectImports + [c.javaType]>
    <#else>
      <#assign thirdPartyImports = thirdPartyImports + [c.javaType]>
    </#if>
  </#if>
</#list>

<#-- 添加必需的import -->
<#assign thirdPartyImports = thirdPartyImports + ["com.baomidou.mybatisplus.annotation.IdType", "com.baomidou.mybatisplus.annotation.TableField", "com.baomidou.mybatisplus.annotation.TableId", "com.baomidou.mybatisplus.annotation.TableName"]>
<#assign projectImports = projectImports + ["com.kexio.common.entity.BaseEntity"]>

<#-- 去重并排序 -->
<#function uniqueSort list>
  <#assign result = []>
  <#list list?sort as item>
    <#if !result?seq_contains(item)>
      <#assign result = result + [item]>
    </#if>
  </#list>
  <#return result>
</#function>

<#assign javaImports = uniqueSort(javaImports)>
<#assign thirdPartyImports = uniqueSort(thirdPartyImports)>
<#assign projectImports = uniqueSort(projectImports)>

<#-- 按顺序输出import -->
<#list javaImports as importClass>
import ${importClass};
</#list>
<#if javaImports?size gt 0 && (thirdPartyImports?size gt 0 || projectImports?size gt 0)>

</#if>
<#list thirdPartyImports as importClass>
import ${importClass};
</#list>
<#if thirdPartyImports?size gt 0 && projectImports?size gt 0>

</#if>
<#list projectImports as importClass>
import ${importClass};
</#list>

<#-- 简化的简单类名函数 -->
<#function getSimpleClassName fullClassName>
  <#if fullClassName?? && fullClassName?contains(".")>
    <#return fullClassName?split(".")?last>
  <#else>
    <#return fullClassName!"String">
  </#if>
</#function>

/**
 * ${table.comment!(table.entityName!'')} - 实体类
 * 
 * 继承BaseEntity获得以下自动功能：
 * - 租户ID自动填充和隔离
 * - 审计字段自动维护（创建人、创建时间、更新人、更新时间）
 * - 软删除支持
 * - 实用工具方法：isNew()、checkDeleted()、markDeleted()等
 * 
 * 注意：
 * - 不使用Lombok，所有getter/setter手写，提升调试友好性
 * - 实体字段使用@TableField注解配置填充策略
 * - 主键字段使用合适的ID生成策略
 * 
 * @author ${user.name!"系统"}
 * @since ${now?string('yyyy-MM-dd HH:mm:ss')}
 * @table ${table.tableName}
 */
@TableName("${table.tableName}")
public class ${table.entityName} extends BaseEntity {
<#list columns as c>
    <#-- 跳过BaseEntity中已有的字段 -->
    <#assign skipField = false>
    <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version" || c.columnName?lower_case == "remark" || c.columnName?lower_case == "remarks")>
        <#assign skipField = true>
    </#if>
    
    <#if !skipField>
    <#if (c.isPk?? && c.isPk) || (c.propertyName?? && c.propertyName?lower_case == "id")>
    <#-- 根据字段类型选择合适的ID策略 -->
    <#if c.javaType?? && (c.javaType?contains("String") || c.javaType == "String")>
    @TableId(type = IdType.ASSIGN_ID)
    <#else>
    @TableId(type = IdType.AUTO)
    </#if>
    </#if>
    <#if ((c.columnName!'')?lower_case) != ((c.propertyName!'')?lower_case) && !((c.isPk?? && c.isPk) || (c.propertyName?? && c.propertyName?lower_case == "id"))>
    @TableField("${c.columnName}")
    </#if>
    <#if c.comment?has_content>
    /**
     * ${c.comment}
     */
    <#else>
    /** ${c.propertyName!'field'} */
    </#if>
    private ${getSimpleClassName(c.javaType)} <#if c.propertyName??>${c.propertyName}<#else>field</#if>;
    </#if>
</#list>

<#-- getters/setters -->
<#list columns as c>
    <#-- 跳过BaseEntity中已有的字段 -->
    <#assign skipField = false>
    <#if c.columnName?? && (c.columnName?lower_case == "tenant_id" || c.columnName?lower_case == "create_by" || c.columnName?lower_case == "create_time" || c.columnName?lower_case == "create_date" || c.columnName?lower_case == "update_by" || c.columnName?lower_case == "update_time" || c.columnName?lower_case == "update_date" || c.columnName?lower_case == "deleted" || c.columnName?lower_case == "version" || c.columnName?lower_case == "remark" || c.columnName?lower_case == "remarks")>
        <#assign skipField = true>
    </#if>
    
    <#if !skipField>
    
    <#if c.comment?has_content>
    /**
     * 获取${c.comment}
     *
     * @return ${c.comment}
     */
    <#else>
    /** 获取${c.propertyName!'field'} */
    </#if>
    public ${getSimpleClassName(c.javaType)} get<#if c.propertyName??>${c.propertyName?cap_first}<#else>Field</#if>() { 
        return this.<#if c.propertyName??>${c.propertyName}<#else>field</#if>; 
    }

    <#if c.comment?has_content>
    /**
     * 设置${c.comment}
     *
     * @param <#if c.propertyName??>${c.propertyName}<#else>field</#if> ${c.comment}
     */
    <#else>
    /** 设置${c.propertyName!'field'} */
    </#if>
    public void set<#if c.propertyName??>${c.propertyName?cap_first}<#else>Field</#if>(${getSimpleClassName(c.javaType)} <#if c.propertyName??>${c.propertyName}<#else>field</#if>) { 
        this.<#if c.propertyName??>${c.propertyName}<#else>field</#if> = <#if c.propertyName??>${c.propertyName}<#else>field</#if>; 
    }
    </#if>
</#list>
}